<template>
  <el-dialog title="儿童基本信息" :visible="value" width="700px" custom-class="dialog-box" @close="onClose" @open="onOpen" :close-on-click-modal="false" :append-to-body="true">
    <el-form ref="form" label-width="140px" @submit.native.prevent="onSubmit">
      <el-row>
        <el-col :span="12" v-for="(item, index) in infoList" :key="index">
          <el-form-item :label="item.attrName" :rules="{ required: item.isRequire == 'T' ? true : false, message: '域名不能为空', trigger: 'blur' }">
            <el-input v-if="item.attrType == 'TEXT'" class="baby_control" v-model="item.values">
              <template v-if="item.attrUnit !== ''" slot="append">{{ item.attrUnit }}</template>
            </el-input>
            <el-input v-if="item.attrType == 'TEXTAREA'" type="textarea" class="baby_control" v-model="item.values"></el-input>
            <el-select v-if="item.attrType == 'SELECT'" class="baby_control" v-model="item.values">
              <el-option v-for="(i, index) in item.attrValue" :key="index" :label="i.valValue" :value="i.valKey"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-button v-show="false" :loading="btnLoading" native-type="submit">提交</el-button>
    </el-form>
    <span slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  props: ["value", "curItem", "detail"],
  data() {
    return {
      infoList: [],
      oldList: [],
      btnLoading: false
    };
  },
  mounted() {
    this.onOpen();
  },
  methods: {
    getExamList() {
      let th = this;
      th.showLoading();
      th.$api.findListByCategoryId({ isOnlyRoot: "T", categoryId: "1000160003" }).then(res => {
        if (res.executed) {
          let list = res.infos;
          // th.form.ageMonth = th.detail.ageMonth;
          let data = [];
          for (let i = 0; i < list.length; i++) {
            let arr = {};
            Object.assign(arr, list[i]);
            arr.values = "";
            data.push(arr);
            th.showLoading(false);
          }
          th.infoList = data;
          th.oldList = data;
        }
      });
    },
    onSubmit() {
      let th = this;
      let add = () => {
        let values = th.saveInfoValues(th.infoList);
        let json = {
          createPersonId: th.adminUser.memberId,
          archivesId: "-1",
          name: th.detail.name,
          sex: th.detail.sex == 1 ? "MAN" : "WOMEN",
          age: th.detail.birthday.replace(/\-/g, "/"),
          patientId: th.detail.patientId,
          values: JSON.stringify(values),
          operatorId: th.adminUser.memberId
        };
        th.btnLoading = true;
        th.$api.insertBasicInfo(json).then(res => {
          if (res.executed) {
            th.$message.success("保存成功");
            th.$emit("success");
            th.onClose();
            th.btnLoading = false;
          } else {
            th.$message.error("保存失败！请联系工作人员！");
            th.btnLoading = false;
            // th.onClose();
          }
        });
      };
      if (th.curItem) {
        add();
      } else {
      }
      //   th.$refs["form"].validate(v => {
      //     if (v) {

      //       // insertBasicInfo
      //     }
      //   });
    },
    onClose() {
      let th = this;
      th.$refs["form"].resetFields();
      th.$emit("input", false);
    },
    onOpen() {
      let th = this;
      th.$nextTick(function() {
        let th = this;
        th.getExamList();
        if (th.curItem) {
          th.infoList = th.curItem;
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped></style>
